<?php
/**
 * Created by PhpStorm.
 * User: wangzb
 * Date: 2016/4/23
 * Time: 16:33
 */
namespace Admin\Controller;
use Think\Page;

class MealController extends BaseController
{
    public function indexAction()
    {
        $Meal = D('Meal');
        $condition = array();
        $count = $Meal->where($condition)->count();
        $Page = new Page($count, 50);
        $show = $Page->show();
        $list = $Meal->where($condition)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        foreach($list as $key => $item) {
            $meal_cats = $Meal->getCatList($item['id']);
            $list[$key]['meal_cats'] = $meal_cats;
        }
        $this->assign('page', $show);
        $this->assign('list', $list);
        $this->display();
    }

    public function addAction()
    {
        if (IS_POST) {
            $Meal = D('Meal');
            $MealCats = D('MealCats');
            $cat_ids = isset($_POST['cat_id']) ? array_unique($_POST['cat_id']) : array();
            if (empty($cat_ids)) {
                $this->error('菜谱需要选择');
            }
            $data = $Meal->create($_POST['Meal']);
            if (false !== $data) {
                $insertId = $Meal->add();
                if (false !== $insertId) {
                    foreach($cat_ids as $val) {
                        $data = array(
                            'meal_id' => $insertId,
                            'cat_id' => $val,
                        );
                        $MealCats->add($data);
                    }
                    $this->success('新增成功', session('referr_url'));
                } else {
                    $this->error('更新失败（失败原因：' . $Meal->getError() . '）');
                }
            } else {
                $this->error($Meal->getError());
            }
        } else {
            $cat_list = D('MealCategory')->getCatList();
            $this->assign('cat_list', $cat_list);
            $this->display();
        }
    }

    public function updateAction()
    {
        $Meal = D('Meal');
        $MealCats = D('MealCats');
        if (IS_POST) {
            $cat_ids = isset($_POST['cat_id']) ? array_unique($_POST['cat_id']) : array();
            if (empty($cat_ids)) {
                $this->error('菜谱需要选择');
            }
            $data = $Meal->create($_POST['Meal']);
            if (false !== $data) {
                if (false !== $Meal->save()) {
                    $MealCats->where(array('meal_id' => $data['id']))->delete();
                    foreach($cat_ids as $val) {
                        $catIdsData = array(
                            'meal_id' => $data['id'],
                            'cat_id' => $val,
                        );
                        $MealCats->add($catIdsData);
                    }
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败（失败原因：' . $Meal->getError() . '）');
                }
            } else {
                $this->error($Meal->getError());
            }
        } else {
            $id = I('get.id', 0, 'intval');
            $info = $Meal->getInfo($id);
            if (false === $info) {
                $this->error('获取信息失败!');
            }
            $this->assign('info', $info);
            $cat_list = D('MealCategory')->getCatList();
            $this->assign('cat_list', $cat_list);
            $this->display();
        }
    }
    
    public function deleteAction()
    {
        $id = I('get.id', 0, 'intval');
        $Meal = M('Meal');
        if (false !== $Meal->delete($id)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }

    /**
     * 编辑器上传处理
     */
    public function uploadJsonAction()
    {
        $config = array(
            'savePath' => C('PICTURE_UPLOAD.rootPath')  . 'meal/',
            'subPath' => 'meal/',
        );
        vendor('Kindeditor.UploadJson', '', '.class.php');
        $UploadJson = new \UploadJson($config);
        $UploadJson->upload();
    }

    /**
     * 编辑器上传文件管理
     */
    public function fileManagerJson()
    {

    }
}